package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.JsonResult;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.java.Log;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        //传入当前页和每页显示的数据条数
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> employees = employeeMapper.selectAll(qo);
        return new PageInfo<>(employees);
    }

    @Override
    public Employee selectOne(Long id) {
        Employee employee = employeeMapper.selectOne(id);
        return employee;
    }


    @Override
    public void insert(Employee employee) {
        employeeMapper.insert(employee);
    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    //查询所有的角色权限
    @Override
    public List<Role> getRoles() {
        List<Role> roles = roleMapper.selectAll();
        return roles;
    }

    //新增用户以及用户角色权限到数据库
    public void save(Employee employee, Long[] roleIds) {
        //新增员工到数据库
        employeeMapper.insert(employee);
        //将员工对应的id以及员工对应的角色权限新增进入数据库
        if (roleIds != null && roleIds.length > 0) {
            employeeMapper.insertRelation(employee.getId(), roleIds);
        }
    }

    //修改用户，若选择了角色权限修改，那么在修改之前应该先删除用户原来拥有的角色权限，再重新加入进去
    public void update(Employee employee, Long[] ids) {
        //调用RoleService的删除方法

        //添加关系
        if (ids != null && ids.length > 0) {
            employeeMapper.insertRelation(employee.getId(),ids);
        }
        //修改用户
        employeeMapper.updateByPrimaryKey(employee);

    }

    public Employee login(String username,String password){
        if(!StringUtils.hasText(username)){
            throw new RuntimeException("用户名不能为空!");
        }
        if (!StringUtils.hasText(password)){
            throw  new RuntimeException("密码不能为空！");
        }
        Employee employee = employeeMapper.selectByUsernameAndPassword(username,password);
        if(employee == null){
            throw  new RuntimeException("账号或者密码错误!");
        }
        return employee;
    }

    //导出
    public Workbook exportXls() {
        //查询所有的员工信息
        List<Employee> employees = employeeMapper.queryAll();
        //生成一个excel,并返回给浏览器
        Workbook wb = new HSSFWorkbook();
        //创建表格
        Sheet sheet = wb.createSheet("员工通讯录");
        //创建标题行
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("真实姓名");
        row.createCell(2).setCellValue("邮箱");
        row.createCell(3).setCellValue("年龄");
        for (int i = 0; i < employees.size(); i++) {
           //获取员工对象
            Employee employee = employees.get(i);
            //创建行
             row = sheet.createRow(i+1);
            //创建单元格(列号)
            //Cell cell = row.createCell(4);
            //写入内容 cell.setCellValue("hello");
            row.createCell(0).setCellValue(employee.getUsername());
            row.createCell(1).setCellValue(employee.getName());
            row.createCell(2).setCellValue(employee.getEmail());
            if (employee.getAge()!=null){
                row.createCell(3).setCellValue(employee.getAge());
            }
        }
        return wb;
    }

    //查询用户名
    public Employee selectByUsername(String username) {
        Employee employee = employeeMapper.selectByUsername(username);
        return employee;
    }

    //查询所有员工
    public List<Employee> list() {
        List<Employee> employees = employeeMapper.queryAll();
        return employees;
    }


    public void updateUserStatus(Long id,Boolean status) {
        employeeMapper.updateUserStatus(id,status);
    }

    @Override
    public void setStatus(Boolean status, Long id) {

    }

    @Override
    public void status(boolean status, Long id) {
        employeeMapper.status(status,id);
    }

    @Override
    public void batchDelete(Long[] ids) {
        employeeMapper.batchDelete(ids);
    }

    @Override
    public void updateById(Long id,String newPassword) {
        employeeMapper.updatePwdById(id,newPassword);
    }


}
